package com.banzhi.shop.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonHeadAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.SaleRanking.Ranking_Data;
import com.banzhi.shop.entity.SaleRanking.SumInfo_Data;
import com.banzhi.shop.model.CallBack.SaleRankingCallBack;
import com.banzhi.shop.model.ISaleRankingModel;
import com.banzhi.shop.model.Impl.SaleRankingImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.view.LoadMoreRecyclerView;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.banzhi.shop.R.id.civ_image;
import static com.banzhi.shop.R.id.iv_jiangpai;
import static com.banzhi.shop.R.id.iv_touxiang;
import static com.banzhi.shop.R.id.recyclerview;
import static com.banzhi.shop.R.id.tv_Ranking;
import static com.banzhi.shop.R.id.tv_money;
import static com.banzhi.shop.R.id.tv_name;
import static com.banzhi.shop.R.id.tv_num;
import static com.banzhi.shop.R.mipmap.jiangpai1;
import static com.banzhi.shop.R.mipmap.jiangpai2;
import static com.banzhi.shop.R.mipmap.jiangpai3;

/**
 * 销售排行
 */
public class SaleRankingActivity extends BaseActivity implements View.OnClickListener, SaleRankingCallBack, SwipeRefreshLayout.OnRefreshListener {
    private ImageView ivback;
    private SwipeRefreshLayout swipeRefreshLayout;
    private ISaleRankingModel model;
    private int page = 1;
    private Ranking_Data data;
    private List<SumInfo_Data> sumInfo_datas = new ArrayList<>();
    private LoadMoreRecyclerView recycleview;
    private LinearLayoutManager layoutmanager;
    private CommonHeadAdapter<SumInfo_Data> adapter;
    private boolean loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sale_ranking);
        Translucent.setTranslucent(this);
        setinits();
        setListeners();
        model = new SaleRankingImpl();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(page, this);
    }

    private void setinits() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_listview);
        swipeRefreshLayout.setColorSchemeResources(R.color.themeColor);
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        recycleview = (LoadMoreRecyclerView) findViewById(R.id.recyclerview);
        layoutmanager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recycleview.setLayoutManager(layoutmanager);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        swipeRefreshLayout.setOnRefreshListener(this);
        recycleview.addOnScrollListener(new EndlessRecyclerOnScrollListener(layoutmanager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(SaleRankingActivity.this, "正在加载,请稍后~");
                model.getData(page, SaleRankingActivity.this);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    /**
     * @param object 获取数据成功
     */
    @Override
    public void ongetDataSuccess(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        data = (Ranking_Data) object;
        if (page == 1) {
            sumInfo_datas.clear();
        } else {
            if (data.getSumInfo().size() == 0) {

                return;
            }
        }
        sumInfo_datas.addAll(data.getSumInfo());
        showDatas();
    }

    private void showDatas() {


        if (adapter == null) {
            adapter = new CommonHeadAdapter<SumInfo_Data>(this, R.layout.item_ranking, R.layout.item_saleranking_head, sumInfo_datas) {
                @Override
                public void convert(ViewHolder holder, SumInfo_Data sumInfo_data, int position) {
                    if (position == 1) {
                        holder.getImageView(iv_jiangpai).setVisibility(View.VISIBLE);
                        holder.getTextView(tv_num).setVisibility(View.GONE);
                        holder.getImageView(iv_jiangpai).setImageResource(jiangpai1);
                    } else if (position == 2) {
                        holder.getImageView(iv_jiangpai).setVisibility(View.VISIBLE);
                        holder.getTextView(tv_num).setVisibility(View.GONE);
                        holder.getImageView(iv_jiangpai).setImageResource(jiangpai2);
                    } else if (position == 3) {
                        holder.getImageView(iv_jiangpai).setVisibility(View.VISIBLE);
                        holder.getTextView(tv_num).setVisibility(View.GONE);
                        holder.getImageView(iv_jiangpai).setImageResource(jiangpai3);
                    } else {
                        holder.getImageView(iv_jiangpai).setVisibility(View.GONE);
                        holder.getTextView(tv_num).setVisibility(View.VISIBLE);
                        holder.setText(tv_num, position + "");
                    }
                    if (ASUtils.isCanUse(sumInfo_data.getAvatar())) {
                        Picasso.with(SaleRankingActivity.this).load(sumInfo_data.getAvatar()).placeholder(R.mipmap.zhanweitu2).into((ImageView) holder.getView(iv_touxiang));
                    }
                    holder.setText(tv_name, sumInfo_data.getNickname());
                    if (ASUtils.isCanUse(sumInfo_data.getSumprice())) {
                        holder.setText(tv_money, NumberUtils.DecimalFormat(sumInfo_data.getSumprice()));
                    }
                }

                @Override
                public void headconvert(HeaderViewHolder holder) {
                    if (ASUtils.isCanUse(MyApp.loginMessage.getString("personAvatar", ""))) {
                        Picasso.with(SaleRankingActivity.this).load(MyApp.loginMessage.getString("personAvatar", "")).placeholder(R.mipmap.zhanweitu2).into((ImageView) holder.getView(civ_image));
                    }
                    holder.setText(tv_name, MyApp.loginMessage.getString("personNickname", ""));
                    holder.setText(tv_Ranking, data.getUser().getRanking());
                    if (ASUtils.isCanUse(data.getUser().getSumprice())) {
                        holder.setText(tv_money, NumberUtils.DecimalFormat(data.getUser().getSumprice()));
                    }
                }
            };
            recycleview.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();

        }

    }

    /**
     * @param object 获取数据失败
     */
    @Override
    public void ongetDataError(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    @Override
    public void onRefresh() {
        page = 1;
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(page, this);
    }

}
